package com.jnu.student;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.jnu.student.view.dayTaskFragment;
import com.jnu.student.view.weekTaskFragment;
import com.jnu.student.view.monthTaskFragment;
import com.jnu.student.view.yearTaskFragment;

import java.util.ArrayList;
import java.util.List;


public class taskFragment extends Fragment {

    private TabLayout mTabLayout;
    private ViewPager2 mViewPager2;
    private List<String> mData = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_task, container, false);
        initData();
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        mTabLayout = rootView.findViewById(R.id.tab_layout);
        mViewPager2 = rootView.findViewById(R.id.view_pager);
        taskPagerAdapter taskPagerAdapter = new taskPagerAdapter(getActivity(), mData);
        mViewPager2.setAdapter(taskPagerAdapter);
        new TabLayoutMediator(mTabLayout, mViewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(mData.get(position));
            }
        }).attach();
    }

    private void initData() {
        mData.add("每日计划");
        mData.add("周计划");
        mData.add("月计划");
        mData.add("年计划");
    }
    public class taskPagerAdapter extends FragmentStateAdapter {

        private List<String> mData;

        public taskPagerAdapter(FragmentActivity activity, List<String> data) {
            super(activity);
            this.mData = data;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            String title = mData.get(position);
            if (title.equals("每日计划")) {
                return dayTaskFragment.newInstance();
            } else if (title.equals("周计划")) {
                return weekTaskFragment.newInstance();
            }else if (title.equals("月计划")) {
                return monthTaskFragment.newInstance();
            }else if (title.equals("年计划")) {
                return yearTaskFragment.newInstance();
            } else {
                return null;
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }
}